package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.impl.impl.IVerifyCodeCacheService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.Assert;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Reference//引用远程调用
    private IVerifyCodeCacheService verifyCodeService;

    /**
     * 验证码功能这里暂时先忽略
     * 后台接收到信息后必须先进行数据合法性校验，如：非空，两次密码输入是否一致等
     * 验证通过后需要填充用户的默认数据（性别，等级，城市等），然后保存用户信息
     * @param phone
     * @param nickname
     * @param password
     * @param rpassword
     * @param verifyCode
     * @return
     */
    public AjaxResult userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        AjaxResult result = new AjaxResult();
        try {
            //1:检查数据的合法性,断言工具类(Assert)
            Assert.hasLength(phone, "手机号码不能为空!");
            Assert.hasLength(nickname, "呢称不能为空!");
            Assert.hasLength(password, "密码不能为空!");
            Assert.hasLength(rpassword, "确认密码不能为空!");
            Assert.hasLength(verifyCode, "验证码不能为空!");
            Assert.equalsCase(password, rpassword, "密码和确认密码必须相同!");

            //2:校对验证码
            String vc = verifyCodeService.getVerifyCode(phone);
            Assert.equalsIgnoreCase(vc, verifyCode, "验证码输入错误!");

            //3:创建UserInfo并设置数据
            UserInfo userInfo=new UserInfo();
            userInfo.setPhone(phone);
            userInfo.setNickname(nickname);
            userInfo.setPassword(password);

            //4:把注册的信息保存到数据库
            userInfoMapper.insert(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    public AjaxResult userLogin(String phone, String password) {

        AjaxResult result = new AjaxResult();

        try {
            //判断参数不能为空
            Assert.hasLength(phone, "手机号码不能为空!");
            Assert.hasLength(password, "密码不能为空!");

            UserInfo userInfo = userInfoMapper.selectByPhoneAndPassword(phone, password);
            if (userInfo == null) {
                return result.mark("账号或密码错误!");
            }

            //将对象的信息添加并返回到controller
            result.addData(userInfo);

        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }

    /**
     * 检查手机号是否存在(要求返回判断结果)
     *
     * @param phone
     * @return true:不存在   false:已经存在
     */
    public Boolean checkPhone(String phone) {
        return userInfoMapper.selectByPhone(phone) == null;
    }

}
